package com.situ.ftbserver.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.situ.ftbserver.mapper.CoachMapper;
import com.situ.ftbserver.model.Coach;
import com.situ.ftbserver.model.Course;
import com.situ.ftbserver.model.search.CoachSearchBean;
import com.situ.ftbserver.service.CoachService;
import com.situ.ftbserver.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

@CacheConfig(cacheNames = "coachServiceImpl")
@Service
public class CoachServiceImpl implements CoachService {
    private CoachMapper coachMapper;
    private CourseService courseService;
    @Autowired
    public void setCoachMapper(CoachMapper coachMapper) {
        this.coachMapper = coachMapper;
    }

    @Autowired
    public void setCourseService(CourseService courseService) {
        this.courseService = courseService;
    }

    @Override
    public Page<Coach> findAll(Page<Coach> page, CoachSearchBean csb) {
        return coachMapper.findAll(page, csb);
    }

    @CacheEvict(allEntries = true)
    @Override
    public int delete(Integer[] ids) {
        List<Course> byCoachId = courseService.findByCoachId(ids);
        if (!byCoachId.isEmpty()) {
            return 0;
        }
        return coachMapper.deleteByIds(Arrays.asList(ids));
    }

    @CacheEvict(allEntries = true)
    @Override
    public boolean save(Coach coach) {
        return coachMapper.insert(coach) > 0;
    }

    @CacheEvict(allEntries = true)
    @Override
    public boolean updateById(Coach coach) {
        return coachMapper.updateById(coach)>0;
    }

    @Cacheable(key = "'findCoach'")
    @Override
    public List<Coach> findCoach() {
        return coachMapper.selectList( null);
    }

    @Override
    public List<Map<String, Integer>> getPopularCoach() {
        return coachMapper.getPopularCoach();
    }
}
